package com.chunyu.ask.util.string;


import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTool
{
  public static String replace(String mobiles, String srcString, String replaceString)
  {
    return mobiles.replace(srcString, replaceString);
  }

  public static boolean isMobile(String mobiles)
  {
    Pattern p = Pattern.compile("^((1))\\d{10}$");

    Matcher m = p.matcher(mobiles);

    System.out.println(m.matches() + "---");

    return m.matches();
  }

  public static boolean isEmail(String email)
  {
    Pattern pattern = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");

    Matcher matcher = pattern.matcher(email);
    if (matcher.matches()) {
      return true;
    }
    return false;
  }

  public static String getMapString(Map paramMap, String mapStr)
  {
    String returnString = (paramMap != null) && (paramMap.get(mapStr) != null) ? paramMap.get(mapStr).toString() : "";

    return returnString;
  }
  
  public static boolean isEmpty(String str)
  {
    return (str == null) || (str.length() == 0);
  }
  
  public static boolean isNotEmpty(String str)
  {
    return !isEmpty(str);
  }

  public static String arrValue(String arrString, String split, int index)
  {
    String v = arrString;
    int i = arrString.indexOf(split);
    if (i > -1) {
      int len = arrString.length();
      if (index == 0)
        v = arrString.substring(0, i);
      else if (index == 1) {
        v = arrString.substring(i + 1, len);
      }
    }
    return v;
  }

  public static Long convertObjectToLong(Object obj) {
    return Long.valueOf(String.valueOf(obj));
  }

  public static String convertObjectToString(Object obj) {
    return String.valueOf(obj);
  }

  public static boolean isLongEmpty(Long l)
  {
    if ((null != null) && (l.longValue() > 0L)) {
      return false;
    }
    return true;
  }

  public static StringBuffer splitTail(StringBuffer str)
  {
    int length = str.length();
    System.out.println("length=" + length);
    System.out.println("ad=" + str.lastIndexOf(","));

    if ((length > 0) && (str.lastIndexOf(",") + 1 == length)) {
      return str.delete(length - 1, length);
    }
    return str;
  }

  public static void main(String[] args)
  {
    StringBuffer str = new StringBuffer("");
    str.append("1313,213,1312");

    System.out.println(splitTail(str));

    System.out.println("email" + isEmail("zybyjy@sohu.com"));
    System.out.println(isMobile("1333011111111"));
  }

  public static String toUnicode(String s)
  {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
      if (s.charAt(i) <= 'Ā')
        sb.append("\\u00");
      else {
        sb.append("\\u");
      }
      sb.append(Integer.toHexString(s.charAt(i)));
    }
    return sb.toString();
  }

  public static String ascii2Native(String str)
  {
    StringBuilder sb = new StringBuilder();
    int begin = 0;
    int index = str.indexOf("\\u");
    while (index != -1) {
      sb.append(str.substring(begin, index));
      sb.append(ascii2Char(str.substring(index, index + 6)));
      begin = index + 6;
      index = str.indexOf("\\u", begin);
    }
    sb.append(str.substring(begin));
    return sb.toString();
  }

  private static char ascii2Char(String str) {
    if (str.length() != 6) {
      throw new IllegalArgumentException("Ascii string of a native character must be 6 character.");
    }

    if (!"\\u".equals(str.substring(0, 2))) {
      throw new IllegalArgumentException("Ascii string of a native character must start with \"\\u\".");
    }

    String tmp = str.substring(2, 4);
    int code = Integer.parseInt(tmp, 16) << 8;
    tmp = str.substring(4, 6);
    code += Integer.parseInt(tmp, 16);
    return (char)code;
  }


  public static String objectToString(Object obj)
  {
    String str = "";
    if (obj != null) {
      str = String.valueOf(obj);
    }
    return str;
  }

}
